<?php
namespace app\library\admin;
use app\admin\controller\Admin;
use app\library\model\Area as AreaModel;
use app\library\model\Seat as SeatModel;
// 区域控制器
class Area extends Admin
{
    public function index()
    {    
    	$this->assign('tab_type', 3);
        $data_list = AreaModel::where('')->order('sort id')->select();
        $data_list = getTree($data_list,0);
        $this->assign('data_list', $data_list);
        return $this->fetch();
    }
    /**
     * 添加区域
     * @return mixed
     */
    public function add(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'area');
            if($result !== true) {
                return $this->error($result);
            }
            unset($data['id']);
        	$model = new AreaModel();  
	        if ($model->isBottomPid($data['pid'])) {
	        	return $this->error('分区栏目下无法添加子馆');
	        }
            $data['sort'] = 100;
            if (!AreaModel::create($data)) {
                return $this->error('添加失败！');
            }
            return $this->success('添加成功。');
        }
        $id = $this->request->param('id');
        if (empty($id)) {
        	$id = 0;
        }
        $row['areatype'] = 1;
        $this->assign('data_info', $row);
        $this->assign('level_option', AreaModel::getAreaOption($id,true));
    	return $this->fetch('form');
    }
    /**
     * 修改区域
     * @return mixed
     */
    public function edit($id = 0){
        $row = AreaModel::where('id', $id)->field('id,pid,areatype,areaname,tag')->find();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'area');
            if($result !== true) {
                return $this->error($result);
            }
        	$model = new AreaModel();  
	        if ($model->isBottomPid($data['pid'])) {
	        	return $this->error('分区栏目下无法添加子馆');
	        }
            if (!AreaModel::update($data)) {
                return $this->error('修改失败！');
            }
            return $this->success('修改成功。');
        }
        $this->assign('data_info', $row);
        $this->assign('level_option', AreaModel::getAreaOption($id,true));
        return $this->fetch('form');
    }

    /**
     * 删除区域
     * @return mixed
     */
    public function del(){
        $id = input('param.id/a');
        $model = new AreaModel();
        if ($model->isExistSub($id[0])) {
        	return $this->error('该栏目存在下级，不能删除');
        }
        if ($model->del($id)) {
            return $this->success('删除成功。');
        }
        return $this->error($model->getError());
    }
    /**
     * 座位布局
     * @return mixed
     */
    public function layout(){
        $area = new AreaModel();
        $seat = new SeatModel();
        if ($this->request->isPost()) {
            $data = $_POST;
            $arr = explode('|', $data['seatarr']);
            // 验证
            $result = $this->validate($data, 'Seat.layout');
            if($result !== true) {
                return $this->error($result);
            }
            //区域添加bgimg 
            if (!empty($data['bgimg'])) {
                $areaData['bgimg'] = $data['bgimg'];
                $areaWhere['id'] = $data['areaid'];
                if (!$area->edit($areaData,$areaWhere)) {
                    return $this->error('背景添加失败');
                }
            }
            //添加座位
            foreach ($arr as $key => $value) {
                $arr = json_decode($value,true);
                $arr['areaid'] = $data['areaid'];
                $arr['seatNum'] = $key+1;
                if (empty($arr['id'])) {
                    unset($arr['id']);
                }
                $seatData[] = $arr;
            }
            if (!$seat->saveData($seatData)) {
                return $this->error('保存失败!');
            }
            return $this->success('保存成功!');
        }
        $id = $this->request->param('id');
        if (empty($id)) {
        	$id = 0;
        }
        $bgimg = $area->get_id_field($id,'bgimg');
        $seatTypeImg = [config('module_library.hard_seat_pic'),config('module_library.soft_seat_pic'),config('module_library.academic_pic'),config('module_library.exchange_pic')];
        $seatlist = $seat->getSeatArr($id);
        foreach ($seatlist as $key => $value) {
            switch ($value['seatType']) {
                case '1':$seatimg = $seatTypeImg[0];break;
                case '2':$seatimg = $seatTypeImg[1];break;
                case '3':$seatimg = $seatTypeImg[2];break;
                case '4':$seatimg = $seatTypeImg[3];break;
                default:$seatimg = '';break;
            }
            $seatlist[$key]['seatimg'] = $seatimg;
        }
        $this->assign('seatlist',$seatlist);
        $this->assign('seatTypeImg',$seatTypeImg);
        $this->assign('bgimg',$bgimg);
        $this->assign('areaid',$id);
    	return $this->fetch('layout');
    }
}